package tv.huan.media.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.media.domain.Album;
import tv.huan.media.mapper.AlbumMapper;
import tv.huan.media.mapper.AuthorizerMapper;
import tv.huan.media.domain.Authorizer;
import tv.huan.media.service.IAuthorizerService;

/**
 * 授权方Service业务层处理
 * 
 * @author admin
 * @date 2019-10-08
 */
@Service
public class AuthorizerServiceImpl implements IAuthorizerService 
{
    @Autowired
    private AuthorizerMapper authorizerMapper;

    @Autowired
    private AlbumMapper albumMapper;

    /**
     * 查询授权方
     * 
     * @param authorizerId 授权方ID
     * @return 授权方
     */
    @Override
    public Authorizer selectAuthorizerById(Long authorizerId)
    {
        return authorizerMapper.selectAuthorizerById(authorizerId);
    }

    /**
     * 查询授权方列表
     * 
     * @param authorizer 授权方
     * @return 授权方
     */
    @Override
    public List<Authorizer> selectAuthorizerList(Authorizer authorizer)
    {
        return authorizerMapper.selectAuthorizerList(authorizer);
    }

    /**
     * 查询所有授权方
     * @return 授权方集合
     */
    @Override
    public List<Authorizer> selectAuthorizerListAll()
    {
        return authorizerMapper.selectAuthorizerListAll();
    }

    /**
     * 查询所有引入商务
     * @return 授权方集合
     */
    @Override
    public List<Authorizer> selectBusinessLeader(){
        return authorizerMapper.selectBusinessLeader();
    }

    /**
     * 新增授权方
     * 
     * @param authorizer 授权方
     * @return 结果
     */
    @Override
    public int insertAuthorizer(Authorizer authorizer)
    {
        authorizer.setCreateTime(DateUtils.getNowDate());
        int sort = authorizerMapper.selectAutoIncrement();
        authorizer.setSort(sort);
        return authorizerMapper.insertAuthorizer(authorizer);
    }

    /**
     * 修改授权方
     * 
     * @param authorizer 授权方
     * @return 结果
     */
    @Override
    public int updateAuthorizer(Authorizer authorizer)
    {
        authorizer.setUpdateTime(DateUtils.getNowDate());
        return authorizerMapper.updateAuthorizer(authorizer);
    }

    /**
     * 删除授权方对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAuthorizerByIds(String ids) throws Exception
    {
        Long[] idArr = Convert.toLongArray(ids);
        int count;
        Album album = new Album();
        for (Long id : idArr){
            album.setAuthorizerId(id);
            count = this.albumMapper.getClassificationOrAuthorizerUseingCount(album);
            if (count > 0){
                throw new Exception("授权方被使用，无法删除");
            }
        }
        return authorizerMapper.deleteAuthorizerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除授权方信息
     * 
     * @param authorizerId 授权方ID
     * @return 结果
     */
    public int deleteAuthorizerById(Long authorizerId)
    {
        return authorizerMapper.deleteAuthorizerById(authorizerId);
    }
}
